# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=glib2
pkgname=(glib2 glib2-devel glib2-docs)
pkgver=2.54.3
pkgrel=1
pkgdesc="Common C routines used by GTK+ and other libs"
url="https://www.gtk.org/"
arch=('i686' 'x86_64')
makedepends=('pkg-config' 'python' 'libxslt-devel' 'docbook-xsl' 'pcre-devel' 'libffi-devel' 'libiconv-devel' 'gamin-devel' 'gtk-doc' 'zlib-devel')
source=(https://download.gnome.org/sources/glib/${pkgver%.*}/glib-$pkgver.tar.xz
        2.36.3-not-win32.patch
        2.36.4-gspawn-cygwin.patch
        2.42.1-goption-cygwin.patch
        2.34.3-m4macros.patch
        2.36.3-visibility.patch
        2.36.4-gutf8-cygwin.patch
        2.38.2-gconvert-cygwin.patch
        2.50-gmodule-cygwin.patch
        glib-2.38.1-msys2.patch)
sha256sums=('963fdc6685dc3da8e5381dfb9f15ca4b5709b28be84d9d05a9bb8e446abac0a8'
            '8f37c92212a34c392066ff5b9952994550895fd6098b01d62f49139fea5b9ec1'
            '5a5df4d7ca0ae2be859a80731041fd9cfe6e1a338a882702d1e7d820816d8866'
            '01bb13f685ffd77c47df4609e8ab394463275d4c58013e5aaf5f7f9b6b442a7e'
            '9095183f944334e2d02ca67b363d43ea7508c2b02c6e91905160786e7bccbaa7'
            '23a544c39a8596dfcd59b0226a3228d04c288c2023f6069942c3a76b8397e93b'
            '5016c0af3a21a03fc1a979a5de21f2116edc75c88f2980e0f2ab816eff98680b'
            '9054ae2ba34a12bcafa7659f9f21badbfa7aa6114657ed993818cd867ba698f4'
            '45a4d5d5fdc01c38d83f7f49a9936dfe7fbaf25e0aa791f1e5c22c89a6c49d93'
            '3030fe15798cef54aebaf5b8c186f2bc87e49f0385198e3e4fe9ab8a57bca1c9')

prepare() {
  cd glib-${pkgver}

  # Cygwin patches
  patch -p2 -i ${srcdir}/2.36.3-not-win32.patch
  patch -p2 -i ${srcdir}/2.36.4-gspawn-cygwin.patch
  patch -p2 -i ${srcdir}/2.42.1-goption-cygwin.patch
  patch -p2 -i ${srcdir}/2.34.3-m4macros.patch
  patch -p2 -i ${srcdir}/2.36.3-visibility.patch
  patch -p2 -i ${srcdir}/2.36.4-gutf8-cygwin.patch
  patch -p2 -i ${srcdir}/2.38.2-gconvert-cygwin.patch
  patch -p2 -i ${srcdir}/2.50-gmodule-cygwin.patch

  # MSYS patches
  patch -p1 -i ${srcdir}/glib-2.38.1-msys2.patch

  libtoolize --force --copy
  autoreconf -fi
}

build() {
  export lt_cv_deplibs_check_method='pass_all'
  cd glib-${pkgver}
  ./configure \
    --build=${CHOST} \
    --host=${CHOST} \
    --prefix=/usr \
    --disable-libelf \
    --with-pcre=system \
    --with-threads=posix \
    --enable-shared \
    --disable-static \
    --disable-silent-rules \
    ac_cv_header_winsock2_h=no \
    ac_cv_header_mswsock_h=no
  make VERBOSE=1

  make completiondir=/usr/share/bash-completion/completions DESTDIR="${srcdir}/dest" install
  pushd ${srcdir}/dest/usr/share/gdb/auto-load/usr/lib > /dev/null
    mv libglib* msys-glib-2.0-0.dll-gdb.py
    mv libgobject* msys-gobject-2.0-0.dll-gdb.py
  popd > /dev/null
}

check() {
  cd glib-${pkgver}
  #make -k check || :
}

package_glib2() {
  depends=('libxslt' 'libpcre' 'libffi' 'libiconv' 'zlib')
  optdepends=('gamin: for gio fam module'
              'python2: for gdbus-codegen and gtester-report')
  options=('!docs' '!emptydirs')
  license=('LGPL')

  #cd glib-${pkgver}
  #make completiondir=/usr/share/bash-completion/completions DESTDIR="${pkgdir}" install

  mkdir -p ${pkgdir}/usr/{bin,lib,share}
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
  for file in gapplication gdbus gio-querymodules glib-compile-schemas gobject-query gsettings; do
    cp -f ${srcdir}/dest/usr/bin/${file}.exe ${pkgdir}/usr/bin/
  done

  cp -rf ${srcdir}/dest/usr/lib/gio ${pkgdir}/usr/lib/
  #cp -rf ${srcdir}/dest/usr/share/doc ${pkgdir}/usr/share/
  cp -rf ${srcdir}/dest/usr/share/gdb ${pkgdir}/usr/share/

  mkdir -p ${pkgdir}/usr/share/{man,glib-2.0}
  cp -rf ${srcdir}/dest/usr/share/glib-2.0/gdb ${pkgdir}/usr/share/glib-2.0/
  cp -rf ${srcdir}/dest/usr/share/glib-2.0/schemas ${pkgdir}/usr/share/glib-2.0/
  cp -rf ${srcdir}/dest/usr/share/locale ${pkgdir}/usr/share/
  #mkdir -p ${pkgdir}/usr/share/man/man1
  #cp -rf ${srcdir}/dest/usr/share/man/man1/gdbus.* ${pkgdir}/usr/share/man/man1/
  #cp -rf ${srcdir}/dest/usr/share/man/man1/gio* ${pkgdir}/usr/share/man/man1/
  #cp -rf ${srcdir}/dest/usr/share/man/man1/glib-compile-schemas* ${pkgdir}/usr/share/man/man1/
  #cp -rf ${srcdir}/dest/usr/share/man/man1/gobject* ${pkgdir}/usr/share/man/man1/
  #cp -rf ${srcdir}/dest/usr/share/man/man1/gsettings.* ${pkgdir}/usr/share/man/man1/

  cp -rf ${srcdir}/dest/usr/share/bash-completion ${pkgdir}/usr/share/

  for _i in "${pkgdir}/usr/share/bash-completion/completions/"*; do
    chmod -x "$_i"
  done
}

package_glib2-devel() {
  pkgdesc="glib2 headers and libraries"
  groups=('development')
  depends=("glib2=${pkgver}" 'pcre-devel' 'libffi-devel' 'libiconv-devel' 'zlib-devel')
  options=('emptydirs')

  mkdir -p ${pkgdir}/usr/{bin,lib,share}
  cp -f ${srcdir}/dest/usr/bin/gdbus-codegen ${pkgdir}/usr/bin/
  cp -f ${srcdir}/dest/usr/bin/glib-* ${pkgdir}/usr/bin/
  rm -f ${pkgdir}/usr/bin/glib-compile-schemas*
  cp -f ${srcdir}/dest/usr/bin/gresource* ${pkgdir}/usr/bin/
  cp -f ${srcdir}/dest/usr/bin/gtester* ${pkgdir}/usr/bin/

  cp -rf ${srcdir}/dest/usr/lib/pkgconfig ${pkgdir}/usr/lib/
  cp -f ${srcdir}/dest/usr/lib/*.a ${pkgdir}/usr/lib/
  #cp -rf ${srcdir}/dest/usr/lib/gio* ${pkgdir}/usr/lib/
  cp -rf ${srcdir}/dest/usr/lib/glib* ${pkgdir}/usr/lib/

  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/share/aclocal ${pkgdir}/usr/share/
  cp -rf ${srcdir}/dest/usr/share/glib-2.0 ${pkgdir}/usr/share/
  rm -rf ${pkgdir}/usr/share/glib-2.0/gdb
  rm -rf ${pkgdir}/usr/share/glib-2.0/schemas

  #mkdir -p ${pkgdir}/usr/share/man/man1
  #cp -rf ${srcdir}/dest/usr/share/man/man1/gdbus-codegen* ${pkgdir}/usr/share/man/man1/
  #cp -rf ${srcdir}/dest/usr/share/man/man1/glib-* ${pkgdir}/usr/share/man/man1/
  #rm -f ${pkgdir}/usr/share/man/man1/glib-compile-schemas*
  #cp -rf ${srcdir}/dest/usr/share/man/man1/gresource* ${pkgdir}/usr/share/man/man1/
  #cp -rf ${srcdir}/dest/usr/share/man/man1/gtester* ${pkgdir}/usr/share/man/man1/
}

package_glib2-docs() {
  pkgdesc="Documentation for glib2"
  conflicts=('gobject2-docs')
  replaces=('gobject2-docs')
  license=('custom')
  options=('docs' '!emptydirs')

  cd glib-$pkgver/docs
  make DESTDIR="${pkgdir}" install
  install -m755 -d "${pkgdir}/usr/share/licenses/glib2-docs"
  install -m644 reference/COPYING "${pkgdir}/usr/share/licenses/glib2-docs/"

  rm -rf "${pkgdir}/usr/share/man"
}
